package com.chtw.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chtw.dao.StudentInfoMapper;
import com.chtw.entity.StudentInfo;
import com.chtw.entity.StudentInfoExample;
import com.chtw.entity.TeacherInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class StudentService {

	@Autowired
	private StudentInfoMapper studentInfoMapper;
	
	@Autowired
	private TeacherService teacherService;
	
	/**
	 * 获取学生信息
	 * @return
	 */
	public List<StudentInfo> getALL(){
		List<StudentInfo> stu = studentInfoMapper.selectByExample(null);
		return stu;
	}

	/**
	 * 删除学生信息
	 * @param parseInt
	 * @return
	 */
	public List<StudentInfo> dele(int stuId) {
		StudentInfoExample example = new StudentInfoExample();
		StudentInfoExample.Criteria cre = example.createCriteria();
		cre.andStuIdEqualTo(stuId);
		int i = studentInfoMapper.deleteByExample(example);
		if(i>0) {
			List<StudentInfo> sList = studentInfoMapper.selectByExample(null);
			return sList;
		}
		return null;
	}
	
	/**
	 * 分页显示数据
	 * @param nowpage
	 * @return
	 */
	public PageInfo<StudentInfo> page(int nowpage){
		PageHelper.startPage(nowpage, 10);
		List<StudentInfo> lists = studentInfoMapper.selectByExample(new StudentInfoExample());
		PageInfo<StudentInfo> info = new PageInfo<StudentInfo>(lists);
		return info;
	}

	/**
	 * 获取系别分类
	 * @return
	 */
	public List<StudentInfo> getAcademy() {
		List<StudentInfo> alist = studentInfoMapper.getByAcademy();
		return alist;
	}
	
	/**
	 * 获取辅导员列表
	 * @return
	 */
	public List<TeacherInfo> getListBytno(){
		List<TeacherInfo> tnoList = teacherService.getListBytno();
		return tnoList;
	}

	/**
	 * 获取入学日期
	 * @return
	 */
	public List<StudentInfo> getintime() {
		List<StudentInfo> intlist = studentInfoMapper.getintime();
		return intlist;
	}

	/**
	 * 根据id获取学生
	 * @param parseInt
	 * @return
	 */
	public StudentInfo getinfo(int stuId) {
		StudentInfo stu = studentInfoMapper.selectByPrimaryKey(stuId);
		return stu;
	}

	/**
	 * 跟新学生信息
	 * @param stu
	 * @param stuId
	 * @return
	 */
	public int updatestu(StudentInfo stu, String stuId) {
		StudentInfoExample example = new StudentInfoExample();
		StudentInfoExample.Criteria cre = example.createCriteria();
		cre.andStuIdEqualTo(Integer.parseInt(stuId));
		return studentInfoMapper.updateByExampleSelective(stu, example);
	}

	/**
	 * 添加学生
	 * @param stu
	 * @return
	 */
	public int addstu(StudentInfo stu) {
		int i = studentInfoMapper.insertSelective(stu);
		return i;
	}

	public List<StudentInfo> search(StudentInfo stu) {
		StudentInfoExample example = new StudentInfoExample();
		StudentInfoExample.Criteria cre = example.createCriteria();
		List<StudentInfo> lists = new ArrayList<StudentInfo>();
		if(stu.getStuId()==null) {
			if(!stu.getStuAcademy().equals("0")&&stu.getTeacherno()==null&&stu.getEntryDate().equals("0")) {
				System.out.println("按照院系查询");
				cre.andStuAcademyEqualTo(stu.getStuAcademy());
			}else if(stu.getStuAcademy().equals("0")&&stu.getTeacherno()!=null&&stu.getEntryDate().equals("0")) {
				System.out.println("按照辅导员查询");
				cre.andTeachernoEqualTo(stu.getTeacherno());
			}else if(stu.getStuAcademy().equals("0")&&stu.getTeacherno()==null&&!stu.getEntryDate().equals("0")) {
				System.out.println("按照入学日期查询");
				cre.andEntryDateEqualTo(stu.getEntryDate());
			}else if(!stu.getStuAcademy().equals("0")&&stu.getTeacherno()!=null&&stu.getEntryDate().equals("0")) {
				System.out.println("按照院系和辅导员查询");
				cre.andStuAcademyEqualTo(stu.getStuAcademy());
				cre.andTeachernoEqualTo(stu.getTeacherno());
			}else if(!stu.getStuAcademy().equals("0")&&stu.getTeacherno()==null&&!stu.getEntryDate().equals("0")) {
				System.out.println("按照院系和入学日期查询");
				cre.andStuAcademyEqualTo(stu.getStuAcademy());
				cre.andEntryDateEqualTo(stu.getEntryDate());
			}else if(stu.getStuAcademy().equals("0")&&stu.getTeacherno()!=null&&!stu.getEntryDate().equals("0")) {
				System.out.println("按照辅导员和入学日期查询");
				cre.andTeachernoEqualTo(stu.getTeacherno());
				cre.andEntryDateEqualTo(stu.getEntryDate());
			}else if(!stu.getStuAcademy().equals("0")&&stu.getTeacherno()!=null&&!stu.getEntryDate().equals("0")) {
				System.out.println("按照院系、辅导员、入学日期查询");
				cre.andStuAcademyEqualTo(stu.getStuAcademy());
				cre.andTeachernoEqualTo(stu.getTeacherno());
				cre.andEntryDateEqualTo(stu.getEntryDate());
			}else {
				System.out.println("无条件查询");
				return studentInfoMapper.selectByExample(null);
			}
			lists = studentInfoMapper.selectByExample(example);
		}else {
			System.out.println("按照学号查询");
			StudentInfo stuinfo = studentInfoMapper.selectByPrimaryKey(stu.getStuId());
			lists.add(stuinfo);
		}
		return lists;
	}

	/**
	 * 含有查询条件的分页
	 * @param nowpage
	 * @param stu
	 * @return
	 */
	public PageInfo<StudentInfo> page(int nowpage, StudentInfo stu) {
		PageHelper.startPage(nowpage, 10);
		StudentInfoExample example = new StudentInfoExample();
		StudentInfoExample.Criteria cre = example.createCriteria();
		if(stu.getStuId()==null) {
			if(!stu.getStuAcademy().equals("0")&&stu.getTeacherno()==null&&stu.getEntryDate().equals("0")) {
				System.out.println("fenye按照院系查询");
				cre.andStuAcademyEqualTo(stu.getStuAcademy());
			}else if(stu.getStuAcademy().equals("0")&&stu.getTeacherno()!=null&&stu.getEntryDate().equals("0")) {
				System.out.println("fenye按照辅导员查询");
				cre.andTeachernoEqualTo(stu.getTeacherno());
			}else if(stu.getStuAcademy().equals("0")&&stu.getTeacherno()==null&&!stu.getEntryDate().equals("0")) {
				System.out.println("fenye按照入学日期查询");
				cre.andEntryDateEqualTo(stu.getEntryDate());
			}else if(!stu.getStuAcademy().equals("0")&&stu.getTeacherno()!=null&&stu.getEntryDate().equals("0")) {
				System.out.println("fenye按照院系和辅导员查询");
				cre.andStuAcademyEqualTo(stu.getStuAcademy());
				cre.andTeachernoEqualTo(stu.getTeacherno());
			}else if(!stu.getStuAcademy().equals("0")&&stu.getTeacherno()==null&&!stu.getEntryDate().equals("0")) {
				System.out.println("fenye按照院系和入学日期查询");
				cre.andStuAcademyEqualTo(stu.getStuAcademy());
				cre.andEntryDateEqualTo(stu.getEntryDate());
			}else if(stu.getStuAcademy().equals("0")&&stu.getTeacherno()!=null&&!stu.getEntryDate().equals("0")) {
				System.out.println("fenye按照辅导员和入学日期查询");
				cre.andTeachernoEqualTo(stu.getTeacherno());
				cre.andEntryDateEqualTo(stu.getEntryDate());
			}else if(!stu.getStuAcademy().equals("0")&&stu.getTeacherno()!=null&&!stu.getEntryDate().equals("0")) {
				System.out.println("fenye按照院系、辅导员、入学日期查询");
				cre.andStuAcademyEqualTo(stu.getStuAcademy());
				cre.andTeachernoEqualTo(stu.getTeacherno());
				cre.andEntryDateEqualTo(stu.getEntryDate());
			}else {
				System.out.println("fenye无条件查询");
			}
		}else {
			cre.andStuIdEqualTo(stu.getStuId());
			System.out.println("fenye按照学号查询");
		}
		List<StudentInfo> lists = studentInfoMapper.selectByExample(example);
		PageInfo<StudentInfo> info = new PageInfo<StudentInfo>(lists);
		return info;
	}
}
